package com.zkyc.framework.website.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.zkyc.framework.common.annotation.NoAuthorization;
import com.zkyc.framework.common.annotation.Timing;
import com.zkyc.framework.common.pojo.WebQuoteContent;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.common.vo.website.*;
import com.zkyc.framework.website.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author bing.bai
 * @create 2022/5/25
 */
@RestController
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentService contentService;


    /**
     * 获取稿件id
     *
     * @return 新增结果
     */
    @GetMapping("queryMaxContentId")
    public ResultVO queryMaxContentId() {
        return contentService.queryMaxContentId();
    }

    /**
     * 获取稿件详情,记录访问量
     *
     * @return 新增结果
     */
    @GetMapping("query")
    @NoAuthorization
    @Timing
    public ResultVO queryContent(Integer id) {
        return contentService.query(id);
    }


    /**
     * 根据标签id获取稿件
     *
     * @return 新增结果
     */
    @GetMapping("contentListByMediaTagId")
    @NoAuthorization
    @Timing
    @HystrixCommand(fallbackMethod = "contentListByTagIdFallback")
    public ResultVO contentListByTagId(Integer size, Integer current, Integer mediaTagId, Integer moudleId, Integer centerId, Integer type) {
        return contentService.contentListByTagId(size, current, mediaTagId, moudleId, centerId, type);
    }

    public ResultVO contentListByTagIdFallback(Integer size, Integer current, Integer mediaTagId, Integer moudleId, Integer centerId, Integer type) {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 获取稿件列表
     *
     * @return 新增结果
     */
    @GetMapping("list")
    @NoAuthorization
    @Timing
    public ResultVO contentList(Integer id, String createdBy, String beginDate,
                                String endDate, String title, Integer status, Integer columnId, Integer tag, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size, @RequestParam(value = "centerId", required = false) Integer centerId
    ) {
        return contentService.list(id, createdBy, beginDate, endDate, title, status, columnId, tag, centerId, current, size);
    }

    /**
     * 获取稿件列表
     *
     * @return 新增结果
     */
    @GetMapping("queryByMoudleId")
    @NoAuthorization
    @Timing
    public ResultVO queryByMoudleId(Integer moudleId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return contentService.queryByMoudleId(moudleId, current, size);
    }

    /**
     * 获取稿件列表在官网
     *
     * @return 新增结果
     */
    @GetMapping("listDashBoard")
    @NoAuthorization
    @Timing
    public ResultVO contentListDashBoard(String beginDate, String endDate, String title, Integer status, Integer columnId, Integer tag, Integer centerId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return contentService.listDashBoard(beginDate, endDate, title, status, columnId, tag, centerId, current, size);
    }

    /**
     * 获取引用稿件列表
     *
     * @return 新增结果
     */
    @GetMapping("quoteList")
    @NoAuthorization
    @Timing
    public ResultVO quoteList(Integer id, String createdBy, String beginDate, String endDate, String title, Integer status, Integer columnId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return contentService.quoteList(id, createdBy, beginDate, endDate, title, status, columnId, current, size);
    }

    /**
     * 官网获取引用稿件列表
     *
     * @return 新增结果
     */
    @GetMapping("quoteListDashBoard")
    @NoAuthorization
    @Timing
    @HystrixCommand(fallbackMethod = "quoteListDashBoardFallback")
    public ResultVO quoteListDashBoard(String beginDate, String endDate, String title, Integer status, Integer columnId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return contentService.quoteListDashBoard(beginDate, endDate, title, status, columnId, current, size);
    }

    public ResultVO quoteListDashBoardFallback(String beginDate, String endDate, String title, Integer status, Integer columnId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 新增稿件
     *
     * @param content 实体
     * @return 新增结果
     */
    @PostMapping("save")
    @Log(type = LogType.内容管理稿件发布, detail = "发布稿件")
    public ResultVO add(@RequestBody WebContentVo content) {
        return contentService.addContent(content);
    }


    /**
     * 新增引用稿件
     *
     * @return 新增结果
     */
    @PostMapping("saveQuote")
    @Log(type = LogType.推荐稿件)
    public ResultVO saveQuote(@RequestBody QuoteVO quoteVO) {
        return contentService.saveQuote(quoteVO);
    }

    /**
     * 查询稿件已被推荐情况
     *
     * @return 新增结果
     */
    @GetMapping("queryQuoteListByContentId")
    public ResultVO queryQuoteListByContentId(Integer contentId) {
        return contentService.queryQuoteListByContentId(contentId);
    }


    /**
     * 置顶推荐稿件
     *
     * @return 新增结果
     */
    @PostMapping("stickQuote")
    public ResultVO stickQuote(@RequestBody WebQuoteContent webQuoteContent) {
        return contentService.stickQuote(webQuoteContent);
    }

    /**
     * 置顶推荐稿件
     *
     * @return 新增结果
     */
    @GetMapping("stick")
    public ResultVO stick(Integer id) {
        return contentService.stick(id);
    }

    /**
     * 解绑引用稿件
     *
     * @return 新增结果
     */
    @PostMapping("deleteQuote")
    @Log(type = LogType.移除稿件)
    public ResultVO deleteQuote(@RequestBody WebQuoteContent webQuoteContent) {
        return contentService.deleteQuote(webQuoteContent);
    }

    /**
     * 修改稿件
     *
     * @param content 实体
     * @return 新增结果
     */
    @PostMapping("update")
    @Log(type = LogType.内容管理稿件编辑, detail = "内容管理稿件编辑")
    public ResultVO update(@RequestBody WebContentVo content) {
        return contentService.update(content);
    }

    /**
     * 删除稿件
     *
     * @return 新增结果
     */
    @PostMapping("delete")
    @Log(type = LogType.内容管理稿件删除, detail = "内容管理稿件删除")
    public ResultVO delete(@RequestBody DeleteContentVo deleteContentVo) {
        return contentService.delete(deleteContentVo);
    }


    /**
     * 获取全局稿件
     */
    @PostMapping("globalList")
    @NoAuthorization
    public ResultVO globalList(@RequestBody ColumnDataPageVo columnDataPageVo) {
        return contentService.globalList(columnDataPageVo);
    }

    /**
     * 替换全局稿件
     */
    @PostMapping("replaceContent")
    @NoAuthorization
    public ResultVO replaceContent(@RequestBody ReplaceContentVo replaceContentVo) {
        return contentService.replaceContent(replaceContentVo);
    }


    /**
     * 获取点击量
     *
     * @return 新增结果
     */
    @PostMapping("clickRateList")
    public ResultVO clickRateList(@RequestBody Integer[] ids) {
        return contentService.clickRateList(ids);
    }

    /**
     * 搜索
     * 匹配不到内容会将搜索内容分词后根据分词结果搜索
     *
     * @param content 搜索内容
     * @return
     */
    @GetMapping("search")
    public ResultVO search(String content,
                           @RequestParam(value = "timeRange", required = false, defaultValue = "0") Integer timeRange,
                           @RequestParam(value = "searchRange", required = false, defaultValue = "0") Integer searchRange,
                           Integer websiteId,
                           @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size
    ) {
        return contentService.search(websiteId, content, current, size, timeRange, searchRange);
    }

    @GetMapping("queryTagByColumnId")
    public ResultVO queryTagByColumnId(String columnId, String centerId) {
        return contentService.queryTagByColumnId(columnId, centerId);
    }

    @GetMapping("queryTagByMoudleId")
    public ResultVO queryTagByMoudleId(String moudleId, String centerId) {
        return contentService.queryTagByMoudleId(moudleId, centerId);
    }

    @GetMapping("queryPicUrl")
    @HystrixCommand(fallbackMethod = "queryPicUrlFallback")
    public ResultVO queryPicUrl(String columnId, String type) {
        return contentService.queryPicUrlById(columnId, type);
    }

    public ResultVO queryPicUrlFallback(String columnId, String type) {
        return ResultVO.success("访问超时,请稍后再试");
    }


    @PostMapping("importExcel")
    public void importExcel(MultipartFile file) {
        contentService.importExcel(file);
    }

    @GetMapping("summaryList")
    public void summaryList() {
        contentService.addSummary();
    }

    @GetMapping("deleteNullList")
    public void deleteNull() {
        contentService.deleteNull();
    }

    @GetMapping("deleteDulList")
    public void deleteDulList() {
        contentService.deleteDulList();
    }

    @GetMapping("timeList")
    public void timeList() {
        contentService.timeList();
    }

    /**
     * 官网获取引用稿件列表
     *
     * @return 新增结果
     */
    @GetMapping("quoteListDashBoardPlus")
    @NoAuthorization
    @Timing
    @HystrixCommand(fallbackMethod = "quoteListDashBoardPlusFallback")
    public ResultVO<?> quoteListDashBoardPlus(String beginDate, String endDate, String title, Integer status, Integer columnId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return contentService.quoteListDashBoardPlus(beginDate, endDate, title, status, columnId, current, size);
    }

    public ResultVO<?> quoteListDashBoardPlusFallback(String beginDate, String endDate, String title, Integer status, Integer columnId, @RequestParam(value = "current", required = false, defaultValue = "0") Integer current, @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ResultVO.success("访问超时,请稍后再试");
    }

    /**
     * 新增稿件
     *
     * @param content 实体
     * @return 提交审核
     */
    @PostMapping("submit")
    @Log(type = LogType.内容管理稿件发布, detail = "提交稿件审核")
    public ResultVO submit(@RequestBody WebContentVo content) {
        return contentService.submit(content);
    }

    /**
     * 稿件下线
     *
     * @return 提交审核
     */
    @GetMapping("offLine")
    public ResultVO offLine(String id) {
        return contentService.offLine(id);
    }


    @GetMapping("updateStatus")
    public ResultVO updateStatus(Integer id) {
        return contentService.updateStatus(id);
    }

    @GetMapping("init")
    public ResultVO init() {
        return contentService.init();
    }
}
